import apiClient from "./apiClient";
import {PAGE_LIMIT} from '../constant'

// 限制条数  页数  偏移量
const LIMIT = PAGE_LIMIT 
const OFFSET = (page)=>{
    return (page-1)*LIMIT
}
// article业务请求封装
export default {

    // 一个文章
    create: article => apiClient.post('/articles', { article }),
    get: slug => apiClient.get(`/articles/${slug}`),
    update: article => apiClient.put('/articles/' + article.slug, { article }),
    delete: slug => apiClient.delete('/articles/' + slug),

    // 喜欢
    favorite: slug => apiClient.post(`/favorites/${slug}`),
    unfavorite: slug => apiClient.delete(`/favorites/${slug}`),

    // 文章列表
    //page =>  limit 限制数量 5，offset : (page-1)*limit = offset
    getAuthor: (author, page) => apiClient.get(`/articles?author=${author}&limit=${5}&offset=${page ? (page - 1) * 5 : 0}`),
    getFavorite: (author, page) => apiClient.get(`/articles?favorite=${author}&limit=${5}&offset=${page ? (page - 1) * 5 : 0}`),
    all: page => apiClient.get(`/articles?limit=${LIMIT}&offset=${OFFSET(page)}`),
    byTag: (tag, page) => apiClient.get(`/articles?tag=${tag}&limit=${LIMIT}&offset=${OFFSET(page)}`),
    followArticles: (page) => apiClient.get(`/articles/follow?limit=${LIMIT}&offset=${OFFSET(page)}`),

}


